package com.deepfos.outreach.component;

import com.deepfos.outreach.domain.CashFlowPushDto;
import com.deepfos.outreach.domain.CashFlowSlot;
import com.yomahub.liteflow.core.NodeComponent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 付款计划查询
 */
@Component("queryDataCmp")
public class QueryDataCmp extends NodeComponent {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void process() throws Exception {
        CashFlowSlot slot = this.getSlot();
        List<String> cfIds = slot.getRequestData();

        String sql = "SELECT cf_id, contract_no, contract_name, lease_type, user_id, lease_start_date, lease_end_date, cf_amount, company_code, supplier_code, cf_date, account_lease_exempt, sys_object_id, sys_contract_id, cf_status_old, cf_vat_invoice_type, period FROM test.cash_flow_push where cf_id IN (:cfIds)";

        Map paramMap = Collections.singletonMap("cfIds", cfIds);

        NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(jdbcTemplate.getDataSource());

        List<CashFlowPushDto> query = template.query(sql, paramMap, new BeanPropertyRowMapper(CashFlowPushDto.class));

        slot.setCfpds(query);
    }

    @Override
    public boolean isAccess() {
        CashFlowSlot slot = this.getSlot();
        if(Objects.nonNull(slot.getRequestData())){
            return true;
        }else{
            return false;
        }
    }
}
